﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LabTurtorial
{
    public partial class Lab03WorkShop : Form
    {
        public Lab03WorkShop()
        {
            InitializeComponent();
        }
        private string vehiclePrice;
        private string detailMessage;
        private void Lab03WorkShop_Load(object sender, EventArgs e)
        {
            tvwVehicle.CollapseAll();
            lswVehicalDetail.Items.Clear();
            nmrQuantity.Value = nmrQuantity.Minimum;
            dudAddonFeature.Text = "(Select)";
            txtTotalAmount.Text = "0";
        }

        private void tvwVehicle_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (tvwVehicle.Nodes[0].Nodes[0].Nodes[0].IsSelected)
            {
                lswVehicalDetail.Items.Clear();
                lswVehicalDetail.Items.Add("Ducati");
                lswVehicalDetail.Items[0].SubItems.Add("2005");
                lswVehicalDetail.Items[0].SubItems.Add("125");
                lswVehicalDetail.Items[0].SubItems.Add("40000");
                lswVehicalDetail.Items.Add("Ducati");
                lswVehicalDetail.Items[1].SubItems.Add("2006");
                lswVehicalDetail.Items[1].SubItems.Add("125");
                lswVehicalDetail.Items[1].SubItems.Add("42000");
            }
            else if (tvwVehicle.Nodes[0].Nodes[0].Nodes[1].IsSelected)
            {
                lswVehicalDetail.Items.Clear();
                lswVehicalDetail.Items.Add("Yamaha");
                lswVehicalDetail.Items[0].SubItems.Add("2006");
                lswVehicalDetail.Items[0].SubItems.Add("150");
                lswVehicalDetail.Items[0].SubItems.Add("52000");

            }
            else if (tvwVehicle.Nodes[0].Nodes[1].Nodes[0].IsSelected)
            {
                lswVehicalDetail.Items.Clear();
                lswVehicalDetail.Items.Add("Honda");
                lswVehicalDetail.Items[0].SubItems.Add("2006");
                lswVehicalDetail.Items[0].SubItems.Add("300");
                lswVehicalDetail.Items[0].SubItems.Add("750000");
                lswVehicalDetail.Items.Add("Honda");
                lswVehicalDetail.Items[1].SubItems.Add("2007");
                lswVehicalDetail.Items[1].SubItems.Add("400");
                lswVehicalDetail.Items[1].SubItems.Add("700000");
            }
            else if (tvwVehicle.Nodes[0].Nodes[1].Nodes[1].IsSelected)
            {
                lswVehicalDetail.Items.Clear();
                lswVehicalDetail.Items.Add("Huyndai");
                lswVehicalDetail.Items[0].SubItems.Add("2006");
                lswVehicalDetail.Items[0].SubItems.Add("300");
                lswVehicalDetail.Items[0].SubItems.Add("800000");
                lswVehicalDetail.Items.Add("Huyndai");
                lswVehicalDetail.Items[1].SubItems.Add("2007");
                lswVehicalDetail.Items[1].SubItems.Add("400");
                lswVehicalDetail.Items[1].SubItems.Add("850000");
            }
            else
            {
                lswVehicalDetail.Items.Clear();
            }
            txtTotalAmount.Text = "0";
            nmrQuantity.Value = nmrQuantity.Minimum;
            dudAddonFeature.Text = "(Select)";
            nmrQuantity.Enabled = false;
            dudAddonFeature.Enabled = false;
        }

        private void lswVehicalDetail_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            if (lswVehicalDetail.SelectedItems.Count > 0)
            {
                double tempPrice = 0.0;
                double totalAmount;
                nmrQuantity.Enabled = true;
                dudAddonFeature.Enabled = true;
                vehiclePrice = e.Item.SubItems[3].Text;
                tempPrice = Convert.ToDouble(vehiclePrice);
                totalAmount = tempPrice * Convert.ToDouble(nmrQuantity.Value);
                txtTotalAmount.Text = totalAmount.ToString();
            }
            else
            {
                nmrQuantity.Enabled = false;
                dudAddonFeature.Enabled = false;
            }
        }

        private void nmrQuantity_ValueChanged(object sender, EventArgs e)
        {
            double tempPrice = 0.0;
            double totalAmount;
            if (lswVehicalDetail.SelectedItems.Count == 0)
            {
                tvwVehicle.Focus();
                return;
            }
            else
            {
                tempPrice = Convert.ToDouble(vehiclePrice);
                totalAmount = tempPrice * Convert.ToDouble(nmrQuantity.Value);
                txtTotalAmount.Text = totalAmount.ToString();
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (lswVehicalDetail.SelectedItems.Count > 0)
            {
                detailMessage = "\n" + "Vehicle: " + tvwVehicle.SelectedNode.Text + "\n" + "Manufacturer:  " + lswVehicalDetail.SelectedItems[0].SubItems[0].Text + "\n" + "Quantity: " + nmrQuantity.Value + "\n" + "Total Amount: " + txtTotalAmount.Text + "\n" + "Addon Features: ";
                if (dudAddonFeature.Text == "(Select)")
                {
                    detailMessage = detailMessage + "None";
                }
                else
                {
                    detailMessage = detailMessage + dudAddonFeature.Text;
                }
                MessageBox.Show("The following details have been saved susscessfully." + detailMessage, "Purchase Order", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Lab03WorkShop_Load(null, null);
            }
            else
            {
                MessageBox.Show("Please select  a vehicle from the list." + detailMessage, "Purchase Order", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
